'use strict';

angular.module('snPaas_om')
	.controller('MysqlDetailCtrl',['$scope','baseUrl','mysqlClusterStore','versionStore','SpSlider','clusterStore','logStore','$window',function($scope,baseUrl,mysqlClusterStore,versionStore,SpSlider,clusterStore,logStore,$window){
		var vm = $scope;
		var deployId = vm.$stateParams.id;
		vm.baseUrl = baseUrl.static;
		vm.config = {
			memMin:1024,
			menInterval:1024
		};
		vm.spMultiTabs = [
			{
				tabAlias: '基本配置',
				tabName: 'mysqlConfigBasic'
			},
			{
				tabAlias: '组件配置',
				tabName: 'mysqlConfig'
			},
			{
				tabAlias: '使用概况',
				tabName: 'mysqlUsage'
			},
			{
				tabAlias: '部署日志',
				tabName: 'deployLog'
			}
		];
		mysqlClusterStore.detail(deployId).then(function(data){
			vm.basicInfo = data;
			// vm.basicInfo.storageMb = data.serviceBroker.memory;
			vm.components = data.components;
		});
		// clusterStore.query().then(function(data){
		// 	console.log(data);
		// 	vm.cfList = data;
		// });
		versionStore.getList(2).then(function(data){
			vm.versions = data;
		});
		logStore.getMysqlList(deployId).then(function(data){
			vm.logList = data;
		});
		var paramCheck = function(){
			for(var i in vm.basicInfo){
        		var t = (vm.basicInfo[i]||'')+'';
        		if(t.length == 0){
        			return false;
        		}
        	}
        	for(var i = 0,l = vm.components.length;i<l;i++){
        		var t = (vm.components[i].netId||'') +'';
        		if(t.length==0){
        			return false;
        		}
        	}
        	return true;
		};

		vm.save = function(){
			if(!paramCheck()){
				SpSlider.slideIn({
					title:'错误',
					content:'请检查参数完整性！',
					type:'alert'
				});
				return;
			}
			mysqlClusterStore.save(vm.basicInfo).then(function(data){


				vm.$state.go('solutions');
				SpSlider.slideIn({
					title:'操作成功',
					content:'集群'+vm.basicInfo.deploymentName+'配置保存成功！',
					type:'noti'
				});
			});
		};
		vm.deploy = function(){
			if(!paramCheck()){
				SpSlider.slideIn({
					title:'错误',
					content:'请检查参数完整性！',
					type:'alert'
				});
				return;
			}
			mysqlClusterStore.deploy(vm.basicInfo).then(function(data){

				vm.$state.go('solutions');
				SpSlider.slideIn({
					title:'操作成功',
					content:'集群'+vm.basicInfo.deploymentName+'部署成功！',
					type:'noti'
				});
			});

		};
		vm.getLog = function(log){
			$window.open('rest/cluster/downloadClusterLog?taskId='+log.taskId);
		};
		return vm;
	}]);